<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>个人注册</title>


    <link rel="stylesheet" type="text/css" href="css/webbase.css" />
    <link rel="stylesheet" type="text/css" href="css/pages-register.css" />
</head>

<body>
	<div class="register py-container ">
		<!--head-->
		<div class="logoArea">
			<a href="" class="logo-bd"></a>
		</div>
		<!--register-->
		<div class="registerArea">
			<h3>注册新用户<span class="go">我有账号，去<a href="/toLogin" target="_blank">登陆</a></span></h3>
			<div class="info">
				<form class="sui-form form-horizontal" id="registerForm">
					<div class="control-group">
						<label class="control-label">用户名：</label>
						<div class="controls">
							<input id="username" name="username" type="text" onblur="checkName()" placeholder="请输入你的用户名" class="input-xfat input-xlarge"><br>
							<span id="s_username"></span>
						</div>
					</div>
					<div class="control-group">
						<label for="inputPassword" class="control-label">登录密码：</label>
						<div class="controls">
							<input type="password" id="password" name="password" onblur="checkPassword()" placeholder="设置登录密码" class="input-xfat input-xlarge"><br>
							<span id="s_password"></span>
						</div>
					</div>
					<div class="control-group">
						<label for="inputPassword" class="control-label">确认密码：</label>
						<div class="controls">
							<input type="password" id="pwd" onblur="checkPwd()" placeholder="再次确认密码" class="input-xfat input-xlarge"><br>
							<span id="ss_password"></span>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">手机号：</label>
						<div class="controls">
							<input type="text" id="phone" name="phone" onblur="checkPhone()" placeholder="请输入你的手机号" class="input-xfat input-xlarge"><br>
							<span id="ss_phone"></span>
						</div>
					</div>
					<div class="control-group">
						<label for="inputPassword" class="control-label">短信验证码：</label>
						<div class="controls">
							<input type="text" id="code" name="code" placeholder="短信验证码" class="input-xfat input-xlarge">
							<a onclick="texting()"><span id="countdown">获取短信验证码</span></a>
						</div>
					</div>
					
					<div class="control-group">
						<label for="inputPassword" class="control-label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
						<div class="controls">
							<input name="m1" type="checkbox" value="2" checked=""><span>同意协议并注册《东东商城用户协议》</span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label"></label>
						<div class="controls btn-reg">
							<a class="sui-btn btn-block btn-xlarge btn-danger" onclick="register()"  target="_blank">完成注册</a> <!--href="home.html"-->
						</div>
					</div>
				</form>
				<div class="clearfix"></div>
			</div>
		</div>
		<!--foot-->
		<div class="py-container copyright">
			<ul>
				<li>关于我们</li>
				<li>联系我们</li>
				<li>联系客服</li>
				<li>商家入驻</li>
				<li>营销中心</li>
				<li>手机东东商城</li>
				<li>销售联盟</li>
				<li>东东商城社区</li>
			</ul>
			<div class="address">地址：北京市昌平区沙阳路18号 邮编：100000 电话：400-0123-302</div>
			<div class="beian">京ICP备08006666号京公网安备11010888888
			</div>
		</div>
	</div>

<script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript" src="js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="js/plugins/jquery-placeholder/jquery.placeholder.min.js"></script>
<script type="text/javascript" src="js/pages/register.js"></script>
</body>

<script>
	//回车键发送表单
	$(function () {
		!//给整个浏览器窗口添加键盘按下事件
		$(window).keydown(function (event) {
			//判断如果按下的是回车键, 则发送请求
			if (event.keyCode===13) {
				register();
			}
		})
	})

	//用户名验证
	function checkName() {
		// 获取username文本输入框的值
		var username = $("#username").val();
		var span = $("#s_username");
		if (username != "") {
			//发送ajax请求
			$.get("/registerController/findUser",{username:username},function (data) {
				if (data.code == 10000){
					if (data.data != 1){
						// 用户名不存在 提示您输入的用户名有误
						span.css("color","green");
						span.html(" ")
					}else {
						//该用户名存在 提示可以登录
						span.css("color","red");
						span.html("此用户名太受欢迎了,请换一个吧")
					}
				}
			})
		}else {
			span.css("color","red");
			span.html("用户名不可以为空")
		}

	}
	//验证密码
	function checkPassword() {
		var password = $("#password").val();
		var span = $("#s_password");
		//2.定义正则
		var reg_password = /^\w{4,10}$/;
		if (password == "") {
			span.css("color","red");
			span.html("密码不可以为空")
		}else if (!reg_password.test(password)){
			span.css("color","red");
			span.html("密码建议长度4-10位")
		}else {
			span.html(" ")
		}
	}
	//验证确认密码
	function checkPwd() {
		var pwd = $("#pwd").val();
		var password = $("#password").val();
		var span = $("#ss_password");
		if (pwd == "") {
			span.css("color","red");
			span.html("确认密码不可以为空")
		}else if (pwd != password){
			span.css("color","red");
			span.html("两次密码不一致,请重新输入")
		}else {
			span.html(" ")
		}
	}
	//验证手机号
	function checkPhone() {
		var phone = $("#phone").val();
		var span = $("#ss_phone");
		//2.定义正则
		var reg_password = /^\w{11}$/;
		if (phone == "") {
			span.css("color","red");
			span.html("手机号不可以为空")
		}else if (!reg_password.test(phone)){
			span.css("color","red");
			span.html("手机号输入有误,请重新输入")
		}else {
			span.html(" ")
		}
	}

	//发送短信
	function texting() {
		var phone = $("#phone").val();
		$.get("/registerController/texting",{phone:phone},function (data) {
			if (data.code == 10000){
				alert("短信发送成功,请稍等")
			}else {
				alert("短信发送错误")
			}
		})

		var time = 59;

		var countdown = setInterval(function () { //倒计时函数
			if (time <= 0 ){
				$("#countdown").text("获取短信验证码");
				clearInterval(countdown)
			}else {
				$("#countdown").text(time-- +"s");
			}
		},1000)
	}

	function register() {
		console.log($("#registerForm").serialize())
		$.get("/registerController/register",$("#registerForm").serialize(),function (data) {
			console.log(data)
			if (data.code == 10000){
				if (data.data > 0){
					alert("注册成功,快去登录吧")
				}else {
					alert("注册失败")
				}
			}else {
				alert(data.message)
			}
		})
	}

</script>
</html>